import pymysql
from setting import MysqlInfo
from loguru import logger

class Mysql(object):

    def __init__(self):
        self.connect = pymysql.connect(**MysqlInfo.connect_info)


    def get_result(self,sql,params=None):
        """
        在数据库中执行SQL，支持insert,update,delete,select
        :param sql:
        :param params:
        :return:
        """
        with self.connect.cursor() as cursor:
            try:
                exe_res = cursor.execute(sql,params)
                if exe_res:
                    result = cursor.fetchall()
                    if 1 == len(result):
                        return result[0]
                    return result
                else:
                    logger.error("SQL执行返回异常，请检查SQL语句:{}".format(sql))
            except Exception as e:
                logger.error("执行异常:{}".format(e))
            finally:
                self.connect.close()


if __name__ == '__main__':

    mysql = Mysql()
    result = mysql.get_result("UPDATE tp_users set user_money = 10000 where mobile = '13088888888'")
    print(result)